<?php
/**
 * Vega Framework
 *
 * @url http://vega.com.vn
 * @url http://clip.vn
 */

/**
 * Abstract class.
 *
 *
 */
abstract class Vega_Widget
{

    /**
     * Name of the Widget
     */
    protected $_name = '';

    protected $_request = null;

    protected $_params = array();

    protected $_loader = null;

    /**
     * View object
     *
     * @var Vega_View
     */
    protected $_view = null;

    protected $_options = array();

    public function __construct(Vega_App $app, array $params = array())
    {
        $this->_app     = $app;
        $this->_params  = $params;
        $this->_request = $app->getRequest();
        $this->_loader  = $app->getLoader();
        $this->_view    = $this->_initView();
    }
    
    /** 
     * Returns the widget's name
     * 
     * @return string
     */
    public function getName()
    {
        return ((empty($this->_name)) ? __CLASS__ : $this->_name);
    }

    protected function _initView()
    {
        $templateDir = $this->_loader->getPath('widget');

        require_once 'Vega/View.php';
        $view = new Vega_View($this->_loader);
        $view->setTemplateDir($templateDir);
        return $view;
    }

    public function setOption($name, $value)
    {
        $this->_options[$name] = $value;
    }

    public function getOption($name, $else = null)
    {
        if (!isset($this->_options[$name]))
        {
            return $else;
        }
        return $this->_options[$name];
    }

    /**
     * Called by the Vega_View::executeWidget()
     *
     * @return unknown
     */
    public function run()
    {
        $this->execute();

        $return = $this->renderOutput();

        return $return;
    }

    /**
     * Render widget output
     *
     * @return string
     */
    public function renderOutput()
    {
        $templateFile = $this->getOption('template', 'default');
        $this->_view->setTemplateFile($templateFile);
        return $this->_view->renderTemplate();
    }

    abstract public function execute();
}


